home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / chap02 / howto06 / delphi10 / drwsutl2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-10-13  |  72.9 KB  |  1,936 lines

  1. unit Drwsutl2;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ShellAPI, FileCtrl, DRWSUtl1;
  8.  
  9. const
  10.   EOC_CHANGEDIR = 1;  { Error Operation Code for change directory failure }
  11.   EOC_SOURCECOPY = 2; { Error Operation Code for source copy failure      }
  12.   EOC_DESTCOPY = 3;   { Error Operation Code for destination copy failure }
  13.   EOC_DELETEFILE = 4; { Error Operation Code for file delete failure      }
  14.   EOC_DELETEDIR = 5;  { Error Operation Code for directory delete failure }
  15.   EOC_RENAMEFILE = 6; { Error Operation Code for renaming failure         }
  16.   EOC_MAKEDIR = 7;    { Error Operation Code for MkDir failure            }
  17.   EOC_SETATTR = 8;    { Error Operation Code for Set Attributes failure   }
  18.  
  19.   FAC_COPY = 1;       { File Action Code for recursive copying            }
  20.   FAC_MOVE = 2;       { File Action Code for recursive moving             }
  21.   FAC_DELETE = 3;     { File Action Code for recursive deletion           }
  22. type
  23.   { This is a descendant of TFileListbox }
  24.   { Which puts icons of files into the   }
  25.   { Objects array rather than the stand- }
  26.   { ard bitmaps.                         }
  27.   TIconFileListBox = class( TFileListBox )
  28.   public
  29.     { public methods and data }
  30.     function GetNextSelection( SourceDirectory : String;
  31.               var CurrentItem : Integer ) : String;
  32.   end;
  33.   TFileWorkBench = class( TComponent )
  34.   public
  35.     GlobalError        : Integer;  { This is used by FMXUCopyFile for er code }
  36.     GlobalErrorType    : Integer;  { This holds the Operation code            }
  37.     function ForceTrailingBackSlash( const TheFileName : String ) : String;
  38.     function StripNonRootTrailingBackSlash(
  39.               const TheFileName : String ) : String;
  40.     procedure GetFileAttributes( TheFile : String; var IsDirectory , IsArchive ,
  41.                 IsVolumeID , IsHidden , IsReadOnly , IsSysFile : Boolean );
  42.     procedure HandleIOException( TheOpCode : Integer; ThePath : String;
  43.                                  TheMessage : String; TheCode : Integer );
  44.     procedure HandleDOSError( TheOpCode : Integer; ThePath : String;
  45.                 TheCode : Integer );
  46.     procedure FMXUCopyFile(const FileName, DestName: String);
  47.     function CopyFile( TargetPath ,
  48.                DestinationPath : String ) : Boolean;
  49.     procedure ChangeTheDirectory( NewPath : String );
  50.     procedure ChangeTheDriveAndDirectory( NewDrive : Integer );
  51.     procedure CopyTheFile( OldPath , NewPath : String );
  52.     procedure MoveTheFile( OldPath , NewPath : String );
  53.     procedure DeleteTheFile( ThePath : String );
  54.     procedure RenameTheFile( OldPath , NewName : String );
  55.     procedure CreateNewDirectory( NewPath : String );
  56.     procedure RemoveDirectory( ThePath : String );
  57.     procedure RecursivelyCopyDirectory( OldPath , NewPath : String );
  58.     procedure RecursivelyMoveDirectory( OldPath , NewPath : String );
  59.     procedure RecursivelyDeleteDirectory( ThePath : String );
  60.     procedure HandleRecursiveAction( StartingPath , NewPath : String;
  61.                ActionCode : Integer );
  62.   end;
  63.   TFileIconPanel = class( TPanel )
  64.   private
  65.     { Private declarations }
  66.     FHighlightColor : TColor;                 { This holds bright edge bevel }
  67.     FShadowColor    : TColor;                 { This holds dark edge bevel   }
  68.     procedure TheClick( Sender : TObject );   { This holds override click    }
  69.     procedure TheDblClick( Sender : TObject );{ This holds override dblclick }
  70.   protected                                   { event method procedure.      }
  71.     { Protected declarations }
  72.     procedure Paint; override;                { This allows custom painting  }
  73.   public
  74.     { Public declarations }
  75.     FTheIcon : TIcon;                         { This is the display icon    }
  76.     FTheName : String;                        { This is the filename        }
  77.     FTheLabel : TLabel;                       { This is the display label   }
  78.     Selected : Boolean;                       { This holds selection status }
  79.     constructor Create(AOwner : TComponent); override; { override create    }
  80.     procedure Initialize( PanelX              ,             { Left          }
  81.                           PanelY              ,             { Top           }
  82.                           PanelWidth          ,             { Width         }
  83.                           PanelHeight         ,             { Height        }
  84.                           PanelBevelWidth     ,             { Bevel Width   }
  85.                           LabelFontSize         : Integer;  { Font size     }
  86.                           PanelColor          ,             { Main color    }
  87.                           PanelHighlightColor ,             { Bright color  }
  88.                           PanelShadowColor    ,             { Dark color    }
  89.                           LabelTextColor        : TColor;   { Text color    }
  90.                           TheFilename         ,             { Filename      }
  91.                           LabelFontName         : String;   { Font name     }
  92.                           LabelFontStyle        : TFontStyles;  { Font style}
  93.                           ExtraData             : Integer       );  { Drive }
  94.     destructor Destroy; override;             { override destroy to free    }
  95.   end;
  96.   TFileIconPanelScrollBox = class( TScrollBox )
  97.   public
  98.     { Public methods and data }
  99.     TheFWB              : TFileWorkBench; { Used for file manipulation         }
  100.     IconsNeedRefreshing : Boolean;                   { Flag to redo display    }
  101.     TheIconSize        : Integer;   { Holds Individual Icon size               }
  102.     TheIconSpacing     : Integer;   { Holds total icon footprint               }
  103.     MaxIconsInARow     : Integer;   { Set for screen size.                     }
  104.     TheStoredHandle    : HWnd;
  105.     procedure Update;                                { Called to reset display }
  106.     constructor Create( AOwner : TComponent ); override;  { Override inherited }
  107.     procedure ClearTheFIPs;                          { Clears the FIPs safely  }
  108.     procedure AddDriveIcons( var XCounter , YCounter : Integer ); { Add drives }
  109.     procedure GetColorsForFileIcon( TheFile : String;
  110.                var BC , HC , SC , TC : TColor );
  111.     procedure GetIconsForEntireDirectory( TargetPath  : String );
  112.     function GetNextSelection( SourceDirectory : String;
  113.               var CurrentItem : Integer ) : String;
  114.     procedure DisplayRecursiveSearchResults(
  115.       TheStartingDirectory : String );
  116.   end;
  117.  
  118.   { This procedure gets an icon for a file using FindExecutable  }
  119.   { and ExtractIcon. (assumes file/dir is passed)                }
  120.   procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
  121.   { This procedure spaces out the bitbtn components on a tpanel }
  122.   procedure SpacePanelButtons( WhichPanel : TPanel );
  123.  
  124. implementation
  125. {$R DRWSUTL2.RES}                 { Import custom resource file }
  126.  
  127. { This procedure spaces out the bitbtn components on a tpanel }
  128. procedure SpacePanelButtons( WhichPanel : TPanel );
  129. var TheCalculatedSpacing     ,            { Holds primary spacing }
  130.     TheFullCalculatedSpacing   : Integer; { Holds full spacing    }
  131.     Counter_1                  : Integer; { Loop counter          }
  132.     TotalIBs                   : Integer; { Gets total buttons    }
  133. begin
  134.   { Set up spacing values }
  135.   TotalIBs := WhichPanel.ControlCount;
  136.   TheCalculatedSpacing := (( WhichPanel.Width - 6 - ( TotalIbs * 49 ))
  137.    div ( TotalIbs + 1 ));
  138.   TheFullCalculatedSpacing := TheCalculatedSpacing + 49;
  139.   { Loop through all imported buttons and set their Left values }
  140.   for Counter_1 := 1 to WhichPanel.ControlCount do
  141.   begin
  142.     if Counter_1 = 1 then
  143.     begin
  144.       TBitBtn( WhichPanel.Controls[ Counter_1 - 1 ] ).Left := 3 +
  145.        TheCalculatedSpacing;
  146.     end
  147.     else
  148.     begin
  149.       TBitBtn( WhichPanel.Controls[ Counter_1 - 1 ] ).Left := 3 +
  150.        (( Counter_1 - 1 ) * TheFullCalculatedSpacing ) + TheCalculatedSpacing;
  151.     end;
  152.   end;
  153. end;
  154.  
  155. { This procedure gets an icon for a file using FindExecutable  }
  156. { and ExtractIcon. (assumes file/dir is passed)                }
  157. procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
  158. var TheExt           : String; { File extension holder }
  159.     TheOtherPChar  ,           { Windows ASCIIZ string }
  160.     TheResultPChar ,           { Windows ASCIIZ string }
  161.     ThePChar         : PChar;  { Windows ASCIIZ string }
  162. begin
  163.   { Check for directory and if so get directory icon from RES file }
  164.   if (( FileGetAttr( TheName ) and faDirectory ) = faDirectory ) then
  165.   begin
  166.     { Set up the PChar to communicate with Windows }
  167.     GetMem( TheOtherPChar , 255 );
  168.     { Convert Pascal-style string to ASCIIZ Pchar }
  169.     StrPCopy( TheOtherPChar , 'DIRECTORY' );
  170.     { Use API call to return icon handle of Icon Resource in FILECTRL.RES }
  171.     TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  172.     { Release memory from PChar }
  173.     FreeMem( TheOtherPChar , 255 );
  174.     { Leave }
  175.     exit;
  176.   end;
  177.   { Assume archive file; get its extension }
  178.   TheExt := Uppercase( ExtractFileExt( TheName ));
  179.   { If not an executable/image file then use FindExecutable to get icon }
  180.   if (( TheExt <> '.EXE' ) and ( TheExt <> '.BAT' ) and
  181.       ( TheExt <> '.PIF' ) and ( TheExt <> '.COM' )) then
  182.   begin
  183.     { Grab three chunks of memory }
  184.     GetMem( TheOtherPChar , 255 );
  185.     GetMem( TheResultPChar , 255 );
  186.     GetMem( ThePChar , 255 );
  187.     { Set up the name and its directory in Windows string formats }
  188.     StrPCopy( ThePChar, TheName );
  189.     StrPCopy( TheOtherPChar , ExtractFilePath( TheName ));
  190.     { Use FindExecutable API call to get path and name of owning file }
  191.     if FindExecutable( ThePChar , TheOtherPChar , TheResultPChar ) > 31 then
  192.     begin
  193.       { If get a result of 32 or more then try to get first icon of owner }
  194.       { Using ExtractIcon API call; 0 indicates first icon.               }
  195.       TheIcon.Handle := ExtractIcon( hInstance , TheResultPchar , 0 );
  196.       { If a handle is 0 then no icon in owner, get default icon from RES file }
  197.       if TheIcon.Handle = 0 then
  198.       begin
  199.         GetMem( TheOtherPChar , 255 );
  200.         StrPCopy( TheOtherPChar , 'NOICON' );
  201.         TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  202.         FreeMem( TheOtherPChar , 255 );
  203.         exit;
  204.       end;
  205.     end
  206.     else
  207.     { if no assigned executable, then get default icon from RES file }
  208.     begin
  209.       GetMem( TheOtherPChar , 255 );
  210.       StrPCopy( TheOtherPChar , 'NOICON' );
  211.       TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  212.       FreeMem( TheOtherPChar , 255 );
  213.       exit;
  214.     end;
  215.     FreeMem( TheOtherPChar , 255 );
  216.     FreeMem( TheResultPChar , 255 );
  217.     FreeMem( ThePChar , 255 );
  218.   end
  219.   else
  220.   { Assume Windows Executable file, so get icon from it with ExtractIcon API }
  221.   begin
  222.     GetMem( ThePChar , 255 );
  223.     StrPCopy( ThePChar , TheName );
  224.     { If no icons in file then get default icon (note use FFFF for -1) }
  225.     if ExtractIcon( hInstance , ThePchar , 65535 ) = 0 then
  226.     begin
  227.       Freemem( ThePChar , 255 );
  228.       GetMem( TheOtherPChar , 255 );
  229.       StrPCopy( TheOtherPChar , 'NOICON' );
  230.       TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  231.       FreeMem( TheOtherPChar , 255 );
  232.       exit;
  233.     end
  234.     else
  235.     begin
  236.       { Try to get first icon for file }
  237.       TheIcon.Handle := ExtractIcon( hInstance , ThePChar , 0 );
  238.       FreeMem( ThePChar , 255 );
  239.       { If handle is 0 invalid icon format so use default from RES file }
  240.       if TheIcon.Handle = 0 then
  241.       begin
  242.         GetMem( TheOtherPChar , 255 );
  243.         StrPCopy( TheOtherPChar , 'NOICON' );
  244.         TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  245.         FreeMem( TheOtherPChar , 255 );
  246.         exit;
  247.       end;
  248.     end;
  249.   end;
  250. end;
  251.  
  252. { This procedure does a fully error-trapped change directory }
  253. procedure TFileWorkBench.ChangeTheDirectory( NewPath : String );
  254. var CurrentDirectory : String;
  255. begin
  256.   if NewPath = '..' then
  257.   begin { Back up one level }
  258.     {$I+}
  259.     try
  260.       { Find the current directory }
  261.       GetDir( 0 , CurrentDirectory );
  262.       { Use EFP to move up one level }
  263.       CurrentDirectory := ExtractFilePath( CurrentDirectory );
  264.       { Strip trailing \ if not root }
  265.       CurrentDirectory := StripNonRootTrailingBackSlash( CurrentDirectory );
  266.       { Try the change to the new drive }
  267.       ChDir( CurrentDirectory );
  268.     except
  269.       { if any exception occurs instantiate exception and show }
  270.       On E:EInOutError do
  271.       begin
  272.         { Call custom error display/lookup procedure }
  273.         HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  274.          E.Message , E.ErrorCode );
  275.       end;
  276.     end;
  277.   end
  278.   else
  279.   begin { Change to explicit path }
  280.     {$I+}
  281.     try
  282.       { Get target directory path }
  283.       CurrentDirectory := NewPath;
  284.       { Strip trailing \ if not root }
  285.       CurrentDirectory := StripNonRootTrailingBackSlash( CurrentDirectory );
  286.       { Try the change to the new drive }
  287.       ChDir( CurrentDirectory );
  288.     except
  289.       { if any exception occurs instantiate exception and show }
  290.       On E:EInOutError do
  291.       begin
  292.         { Call custom error display/lookup procedure }
  293.         HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  294.          E.Message , E.ErrorCode );
  295.       end;
  296.     end;
  297.   end;
  298. end;
  299.  
  300. { This procedure does a fully error-trapped change directory }
  301. procedure TFileWorkBench.ChangeTheDriveAndDirectory( NewDrive : Integer );
  302. var CurrentDirectory : String;
  303. begin
  304.   {$I+}
  305.   try
  306.     { Find the working directory on new drive }
  307.     GetDir( NewDrive , CurrentDirectory );
  308.     { Try the change to the new drive }
  309.     ChDir( CurrentDirectory );
  310.   except
  311.     { if any exception occurs instantiate exception and show }
  312.     On E:EInOutError do
  313.     begin
  314.       { Call custom error display/lookup procedure }
  315.       HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  316.        E.Message , E.ErrorCode );
  317.     end;
  318.   end;
  319. end;
  320.  
  321. { This procedure copies a single file with error trapping }
  322. procedure TFileWorkBench.CopyTheFile( OldPath , NewPath : String );
  323. var AResult : Boolean; { Internal data flag }
  324. begin
  325.   { If Copyfile returns false an error occurred }
  326.   AResult := CopyFile( OldPath , NewPath +
  327.    ExtractFileName( OldPath ));
  328.   { Display meaningful error message }
  329.   if not AResult then HandleDOSError( GlobalErrorType ,
  330.    ExtractFileName( OldPath ) , GlobalError );
  331. end;
  332.  
  333. { This procedure moves a file by copying and delete it }
  334. procedure TFileWorkBench.MoveTheFile( OldPath , NewPath : String );
  335. var AResult : Boolean; { Internal data flag }
  336.     TheFile : File;    { Use to get errors  }
  337. begin
  338.   { If Copyfile returns false an error occurred }
  339.   AResult := CopyFile( OldPath , NewPath +
  340.     ExtractFileName( OldPath ));
  341.   { Display meaningful error message }
  342.   if not AResult then HandleDOSError( GlobalErrorType ,
  343.    ExtractFileName( OldPath ), GlobalError );
  344.   { After valid copying, delete source file }
  345.   {$I+}
  346.   if AResult then try
  347.     { Use this trick to get valid exception handling }
  348.     AssignFile( TheFile , OldPath );
  349.     { Use erase because Deletefile doesn't give exceptions! }
  350.     Erase( TheFile );
  351.   except
  352.     { if any exception occurs instantiate exception and show }
  353.     On E:EInOutError do
  354.     begin
  355.       { Call custom error display/lookup procedure }
  356.       HandleIOException( EOC_DELETEFILE , ExtractFileName( OldPath ) ,
  357.        E.Message , E.ErrorCode );
  358.     end;
  359.   end;
  360. end;
  361.  
  362. { This procedure safely deletes a single file }
  363. procedure TFileWorkBench.DeleteTheFile( ThePath : String );
  364. var TheFile : File; { Internal file handle }
  365. begin
  366.   {$I+}
  367.   try
  368.     { Use this trick to get valid exception handling }
  369.     AssignFile( TheFile , ThePath );
  370.     { Use erase because Deletefile doesn't give exceptions! }
  371.     Erase( TheFile );
  372.   except
  373.     { if any exception occurs instantiate exception and show }
  374.     On E:EInOutError do
  375.     begin
  376.       { Call custom error display/lookup procedure }
  377.       HandleIOException( EOC_DELETEFILE , ExtractFileName( ThePath ) ,
  378.        E.Message , E.ErrorCode );
  379.     end;
  380.   end;
  381. end;
  382.  
  383. { This procedure renames a file with full error trapping }
  384. procedure TFileWorkBench.RenameTheFile( OldPath , NewName : String );
  385. var TheFile : File; { Internal file handle }
  386. begin
  387.   {$I+}
  388.   try
  389.     { Use this trick to get valid exception handling }
  390.     AssignFile( TheFile , OldPath );
  391.     { Use this because RenameFile doesn't give exceptions! }
  392.     Rename( TheFile , NewName );
  393.   except
  394.     { if any exception occurs instantiate exception and show }
  395.     On E:EInOutError do
  396.     begin
  397.       { Call custom error display/lookup procedure }
  398.       HandleIOException( EOC_RENAMEFILE , ExtractFileName( OldPath ) ,
  399.        E.Message , E.ErrorCode );
  400.     end;
  401.   end;
  402. end;
  403.  
  404. { This procedure creates a new directory with full error trapping }
  405. procedure TFileWorkBench.CreateNewDirectory( NewPath : String );
  406. begin
  407.   {$I+}
  408.   try
  409.     Mkdir( NewPath );
  410.   except
  411.     { if any exception occurs instantiate exception and show }
  412.     On E:EInOutError do
  413.     begin
  414.       { Call custom error display/lookup procedure }
  415.       HandleIOException( EOC_MAKEDIR , ExtractFileName( NewPath ) ,
  416.        E.Message , E.ErrorCode );
  417.     end;
  418.   end;
  419. end;
  420.  
  421. { This procedure remove a directory with full error trapping }
  422. procedure TFileWorkBench.RemoveDirectory( ThePath : String );
  423. begin
  424.   {$I+}
  425.   try
  426.     Rmdir( ThePath );
  427.   except
  428.     { if any exception occurs instantiate exception and show }
  429.     On E:EInOutError do
  430.     begin
  431.       { Call custom error display/lookup procedure }
  432.       HandleIOException( EOC_DELETEDIR , ExtractFileName( ThePath ) ,
  433.        E.Message , E.ErrorCode );
  434.     end;
  435.   end;
  436. end;
  437.  
  438. { This procedure recursively copies a directory to a new path }
  439. procedure TFileWorkBench.RecursivelyCopyDirectory( OldPath , NewPath : String );
  440. var TheDir : String; { Holds source directory }
  441. begin
  442.   { Get the source directory to copy }
  443.   TheDir := ExtractFileName( OldPath );
  444.   { Force a backslash to the newpath variable }
  445.   NewPath := ForceTrailingBackSlash( NewPath );
  446.   { Add the source directory to the target path }
  447.   NewPath := NewPath + TheDir;
  448.   { Create a new directory with the new name }
  449.   CreateNewDirectory( NewPath );
  450.   { Force a backslash for compatibility }
  451.   NewPath := FOrcetrailingBackSlash( NewPath );
  452.   { Do the recursive call }
  453.   HandleRecursiveAction( OldPath , NewPath , FAC_COPY );
  454. end;
  455.  
  456. { This procedure recursively moves a directory tree }
  457. procedure TFileWorkBench.RecursivelyMoveDirectory( OldPath , NewPath : String );
  458. var TheDir    : String; { Holds source directory  }
  459.     SavedPath : String; { Holds saved dir to kill }
  460. begin
  461.   { Get the source directory to move }
  462.   TheDir := ExtractFileName( OldPath );
  463.   { Force a backslash to the newpath variable }
  464.   NewPath := ForceTrailingBackSlash( NewPath );
  465.   { Save the starting path just in case }
  466.   SavedPath := OldPath;
  467.   { Add the source directory to the target path }
  468.   NewPath := NewPath + TheDir;
  469.   { Create a new directory with the new name }
  470.   CreateNewDirectory( NewPath );
  471.   { Force a backslash for compatibility }
  472.   NewPath := FOrcetrailingBackSlash( NewPath );
  473.   { Do the recursive call }
  474.   HandleRecursiveAction( OldPath , NewPath , FAC_MOVE );
  475.   { Remove the source directory }
  476.   RemoveDirectory( SavedPath );
  477. end;
  478.  
  479. { This procedure handles recursively deleting an entire directory tree }
  480. procedure TFileWorkBench.RecursivelyDeleteDirectory( ThePath : String );
  481. begin
  482.   HandleRecursiveAction( ThePath , '' , FAC_DELETE );
  483. end;
  484.  
  485.  
  486. { This is the generic routine to copy, move, and delete whole directory trees }
  487. procedure TFileWorkBench.HandleRecursiveAction( StartingPath , NewPath : String;
  488.            ActionCode : Integer );
  489. { VITAL!!! These variables MUST be local for recursrion to work! }
  490. var
  491.     Finished        : Boolean;         { Loop flag              }
  492.     TheSR           : TSearchRec;      { Searchrecord for FF/FN }
  493.     TheResult       : Integer;         { return variable        }
  494.     TargetPath ,
  495.     FileMask   ,
  496.     TheWorkingDirectory ,
  497.     TheStoredWorkingDirectory ,
  498.     ModifiedDirectory  : String;       { path for FF/FN         }
  499.     TheFIP          : TFileIconPanel;  { generic FIP holder     }
  500.     ButtonColor   ,                    { main panel color       }
  501.     ButtonHLColor ,                    { bright panel color     }
  502.     ButtonSColor  ,                    { dark panel color       }
  503.     Textcolor       : TColor;          { label text color       }
  504.     TheFile         : File;
  505.  
  506. begin
  507.   { Set up the initial variables }
  508.   Finished := false;
  509.   TheWorkingDirectory := StartingPath;
  510.   TheStoredWorkingDirectory := TheWorkingDirectory;
  511.   TheWorkingDirectory := TheWorkingDirectory + '\*.*';
  512.   TargetPath := ExtractFilePath( TheWorkingDirectory );
  513.   { Make the call to FindFirst set to get any file }
  514.   TheResult := FindFirst( TheWorkingDirectory , faAnyFile , TheSR );
  515.   { loop through all files in the directory and delete them }
  516.   while not Finished do
  517.   begin
  518.     { Make call to FindNext, using only SearchRecord from FindFirst }
  519.     TheResult := FindNext( TheSR );
  520.     { A -1 result means no more files so exit }
  521.     if TheResult < 0 then finished := true else
  522.     begin
  523.       if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory )
  524.        <> faDirectory ) then
  525.       begin { A File }
  526.         case ActionCode of
  527.           FAC_COPY :
  528.               begin
  529.                 CopyTheFile( TargetPath + TheSR.Name , NewPath );
  530.               end;
  531.           FAC_MOVE :
  532.               begin
  533.                 MoveTheFile( TargetPath + TheSR.Name , NewPath );
  534.               end;
  535.           FAC_DELETE :
  536.               begin { Delete }
  537.                 if MessageDlg( 'Delete file ' + TargetPath + TheSR.Name + '?',
  538.                    mtConfirmation , mbYesNoCancel , 0 ) = mrYes then
  539.                     DeleteTheFile( TargetPath + TheSR.Name );
  540.               end;
  541.         end;
  542.       end;
  543.     end;
  544.   end;
  545.   { Call FindClose for Windows NT/Windows 95 compatibility }
  546.   FindClose( TheSR );
  547.   { Set up the variables to do recursive calls on all directories}
  548.   Finished := false;
  549.   ModifiedDirectory := TheStoredWorkingdirectory + '\*.*';
  550.   { Make the call to FindFirst set to get any file, ignore result }
  551.   TheResult := FindFirst( ModifiedDirectory , faDirectory , TheSR );
  552.   while not Finished do
  553.   begin
  554.     { Make call to FindNext, using only SearchRecord from FindFirst }
  555.     TheResult := FindNext( TheSR );
  556.     { A -1 result means no more files so exit }
  557.     if TheResult < 0 then
  558.       finished := true
  559.     else
  560.     begin
  561.       if TheSR.Name <> '..' then { Ignore backup in this case }
  562.       begin
  563.         if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory )
  564.          = faDirectory ) then
  565.         begin
  566.           { Send in the new directory name }
  567.           ModifiedDirectory := TheStoredWorkingDirectory  + '\' +
  568.            TheSR.Name;
  569.           { Reproduce directory structure for recursion in copy/move }
  570.           NewPath := NewPath + TheSR.Name;
  571.           case ActionCode of
  572.             FAC_COPY , FAC_MOVE :
  573.                begin { Create ahead for move and copy }
  574.                  { Make the new directory for moving and copying }
  575.                  CreateNewDirectory( NewPath );
  576.                  { Force a backslash for compatibility }
  577.                  NewPath := ForceTrailingBackSlash( NewPath );
  578.                end;
  579.             FAC_DELETE :
  580.                begin  { No prior action needed for Delete }
  581.                end;
  582.           end;
  583.           { Do the recursive call }
  584.           HandleRecursiveAction( ModifiedDirectory , NewPath , ActionCode );
  585.           case ActionCode of
  586.             FAC_COPY :
  587.                begin { no action for copy }
  588.                end;
  589.             FAC_MOVE , FAC_DELETE :
  590.                begin  { Delete }
  591.                  { Get a confirmation }
  592.                  if MessageDlg( 'Remove Directory ' + TargetPath + TheSR.Name
  593.                   + '?', mtConfirmation , mbYesNoCancel , 0 ) = mrYes then
  594.                    RemoveDirectory( TargetPath + TheSR.Name );
  595.                end;
  596.           end;
  597.         end;
  598.       end;
  599.     end;
  600.   end;
  601. end;
  602.  
  603. { This is a generic copy routine taken from Delphi sample code }
  604. { It has been edited to return viable error codes!             }
  605. procedure TFileWorkBench.FMXUCopyFile(const FileName, DestName: String);
  606. var
  607.   CopyBuffer: Pointer; { buffer for copying }
  608.   BytesCopied: Longint;
  609.   TheAttr : Integer;
  610.   Source, Dest: Integer; { handles }
  611. const
  612.   ChunkSize: Longint = 8192; { copy in 8K chunks }
  613. begin
  614.   GetMem(CopyBuffer, ChunkSize); { allocate the buffer }
  615.   Source := FileOpen(FileName, fmShareDenyWrite); { open source file }
  616.   if Source < 0 then
  617.   begin  { error creating source file }
  618.     GlobalErrorType := EOC_SOURCECOPY;
  619.     GlobalError := -IOResult;
  620.     if GlobalError = 0 then GlobalError := -157;
  621.     FreeMem( CopyBuffer, ChunkSize );
  622.     exit;
  623.   end;
  624.   Dest := FileCreate(DestName); { create output file; overwrite existing }
  625.   if Dest < 0 then
  626.   begin  { error creating destination file }
  627.     FileClose( Source );
  628.     GlobalErrorType := EOC_DESTCOPY;
  629.     GlobalError := -IOResult;
  630.     if GlobalError = 0 then GlobalError := -159;
  631.     FreeMem( CopyBuffer , ChunkSize );
  632.     exit;
  633.   end;
  634.   {$I-}
  635.   repeat
  636.     BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); { read chunk}
  637.     if BytesCopied > 0 then { if we read anything... }
  638.     FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk }
  639.   until BytesCopied < ChunkSize; { until we run out of chunks }
  640.   {$I+}
  641.   GlobalError := -IOResult;  { get any error code which happens during copying }
  642.   FileClose(Dest); { close the destination file }
  643.   FileClose(Source); { close the source file }
  644.   FreeMem(CopyBuffer, ChunkSize); { free the buffer }
  645. end;
  646.  
  647. { This function calls the sample Copy code and handles errors }
  648. function TFileWorkBench.CopyFile( TargetPath ,
  649.           DestinationPath : String ) : Boolean;
  650. begin
  651.   { Set global error value to no error }
  652.   GlobalError := 0;
  653.   { Call the sample procedure to do the copy }
  654.   FMXUCopyFile( TargetPath, DestinationPath );
  655.   { If no error return true else return false }
  656.   if GlobalError < 0 then CopyFile := false else
  657.    CopyFile := true;
  658. end;
  659.  
  660. { This procedure handles displaying a user-friendly Dialog box with a }
  661. { Message for Delphi IO exception errors.                             }
  662. procedure TFileWorkBench.HandleIOException( TheOpCode : Integer;
  663.            ThePath : String; TheMessage : String; TheCode : Integer );
  664. var ErrorMessageString : String;  { Holds internal data }
  665.     OperationString    : String;  { Holds internal data }
  666. begin
  667.   { clear to check for unrecognized code }
  668.   ErrorMessageString := '';
  669.   { Check against imported code }
  670.   case TheCode of
  671.     2    : ErrorMessageString := 'File not found';
  672.     3    : ErrorMessageString := 'Path not found';
  673.     4    : ErrorMessageString := 'Too many open files';
  674.     5    : ErrorMessageString := 'File access denied';
  675.     6    : ErrorMessageString := 'Invalid file handle';
  676.     12    : ErrorMessageString := 'Invalid file access code';
  677.     15    : ErrorMessageString := 'Invalid drive number';
  678.     16  : ErrorMessageString := 'Cannot remove current directory';
  679.     17    : ErrorMessageString := 'Cannot rename across drives';
  680.     100    : ErrorMessageString := 'Disk read error';
  681.     101    : ErrorMessageString := 'Disk write error';
  682.     102    : ErrorMessageString := 'File not assigned';
  683.     103    : ErrorMessageString := 'File not open';
  684.     104    : ErrorMessageString := 'File not open for input';
  685.     105    : ErrorMessageString := 'File not open for output';
  686.   end;
  687.   case TheOpCode of
  688.     EOC_CHANGEDIR : OperationString := 'Unable to Change Directory due to ';
  689.     EOC_SOURCECOPY : OperationString := 'Unable to Copy due to Source File ';
  690.     EOC_DESTCOPY : OperationString := 'Unable to Copy due to Destination File ';
  691.     EOC_DELETEFILE : OperationString := 'Unable to Delete File due to ';
  692.     EOC_DELETEDIR : OperationString := 'Unable to Delete Directory due to ';
  693.     EOC_RENAMEFILE : OperationString := 'Unable to Rename File due to ';
  694.     EOC_MAKEDIR : OperationString := 'Unable to Create New Directory due to ';
  695.     EOC_SETATTR : OperationString := 'Unable to Set File Attributes due to ';
  696.   end;
  697.   { If not recognized use message; not a DOS error; reset cursor for neatness }
  698.   if ErrorMessageString = '' then
  699.   begin
  700.     Screen.Cursor := crDefault;
  701.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' ' +
  702.      TheMessage , mtError , [mbOK],0);
  703.   end
  704.   else
  705.   begin
  706.     { Recognized DOS exception, reset cursor for neatness }
  707.     Screen.Cursor := crDefault;
  708.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' ' +
  709.      ErrorMessageString , mtError , [mbOK], 0 );
  710.   end;
  711. end;
  712.  
  713. { This procedure handles displaying a user-friendly Dialog box with a }
  714. { Message for DOS error codes.                                        }
  715. procedure TFileWorkBench.HandleDOSError( TheOpCode : Integer;
  716.            ThePath : String;  TheCode : Integer );
  717. var ErrorMessageString : String;  { internal message holder }
  718.     OperationString : String;     { internal message holder }
  719. begin
  720.   { clear the message holder to check for unrecognized code }
  721.   ErrorMessageString := '';
  722.   { Negate the code back to normal number and check to set string }
  723.   case -TheCode of
  724.     2    : ErrorMessageString := 'File not found';
  725.     3    : ErrorMessageString := 'Path not found';
  726.     4    : ErrorMessageString := 'Too many open files';
  727.     5    : ErrorMessageString := 'File access denied';
  728.     6    : ErrorMessageString := 'Invalid file handle';
  729.     12    : ErrorMessageString := 'Invalid file access code';
  730.     15    : ErrorMessageString := 'Invalid drive number';
  731.     16  : ErrorMessageString := 'Cannot remove current directory';
  732.     17    : ErrorMessageString := 'Cannot rename across drives';
  733.     100    : ErrorMessageString := 'Disk read error';
  734.     101    : ErrorMessageString := 'Disk write error';
  735.     102    : ErrorMessageString := 'File not assigned';
  736.     103    : ErrorMessageString := 'File not open';
  737.     104    : ErrorMessageString := 'File not open for input';
  738.     105    : ErrorMessageString := 'File not open for output';
  739.     157 : ErrormessageString := 'Could not open Source File';
  740.     159 : ErrormessageString := 'Could not open Target File';
  741.   end;
  742.   case TheOpCode of
  743.     EOC_CHANGEDIR : OperationString := 'Unable to Change Directory due to ';
  744.     EOC_SOURCECOPY : OperationString := 'Unable to Copy due to Source File ';
  745.     EOC_DESTCOPY : OperationString := 'Unable to Copy due to Destination File ';
  746.     EOC_DELETEFILE : OperationString := 'Unable to Delete File due to ';
  747.     EOC_DELETEDIR : OperationString := 'Unable to Delete Directory due to ';
  748.     EOC_RENAMEFILE : OperationString := 'Unable to Rename File due to ';
  749.     EOC_MAKEDIR : OperationString := 'Unable to Create New Directory due to ';
  750.     EOC_SETATTR : OperationString := 'Unable to Set File Attributes due to ';
  751.   end;
  752.   { If the string is empty an unrecognized code was sent in }
  753.   if ErrorMessageString = '' then
  754.   begin
  755.     { Sent up db based on source or target error; reset cursor for neatness }
  756.     Screen.Cursor := crDefault;
  757.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' Error Code: ' +
  758.      IntToStr( TheCode ) , mtError , [mbOK],0);
  759.   end
  760.   else  { Code is recognized, use message from case statement }
  761.   begin
  762.     { Format the output for source or target error }
  763.     Screen.Cursor := crDefault;
  764.     MessageDlg( OperationString + ExtractFilePath( ThePath ) + ' ' +
  765.      ErrorMessageString , mtError , [mbOK], 0 );
  766.   end;
  767. end;
  768.  
  769. { This procedure sets the imported booleans to the file's attributes }
  770. procedure TFileWorkBench.GetFileAttributes( TheFile : String; var IsDirectory ,
  771.            IsArchive , IsVolumeID , IsHidden , IsReadOnly ,
  772.             IsSysFile : Boolean );
  773. var TheResult : Integer; { Traps for error code on VolumeID }
  774. begin
  775.   { Clear the imported flags for default }
  776.   IsDirectory := false;
  777.   IsArchive := false;
  778.   IsVolumeID := false;
  779.   IsHidden := False;
  780.   IsReadOnly := false;
  781.   IsSysFile := false;
  782.   { Make the Dos call }
  783.   TheResult := FileGetAttr( TheFile );
  784.   if TheResult < 0 then
  785.   begin
  786.     { Volume ID returns -2 (?) }
  787.     IsVolumeID := true;
  788.     { It has no other properties }
  789.     exit;
  790.   end;
  791.   { Use AND test to set all other properties }
  792.   if (( TheResult and faDirectory ) = faDirectory ) then IsDirectory := true;
  793.   if (( TheResult and faArchive ) = faArchive ) then IsArchive := true;
  794.   if (( TheResult and faVolumeID ) = faVolumeID ) then IsVolumeID := true;
  795.   if (( TheResult and faReadOnly ) = faReadOnly ) then IsReadOnly := true;
  796.   if (( TheResult and faHidden ) = faHidden ) then IsHidden := true;
  797.   if (( TheResult and faSysFile ) = faSysFile ) then IsSysFile := true;
  798. end;
  799.  
  800. { This function makes sure a pathname has a trailing \ }
  801. function TFileWorkBench.ForceTrailingBackSlash(
  802.           const TheFileName : String ) : String;
  803. var TempString : String;  { Used to hold function result }
  804. begin
  805.   { If no trailing \ add one (root will already have one.) }
  806.   if TheFileName[ Length( TheFileName ) ] <> '\' then
  807.    TempString := TheFileName + '\' else TempString := TheFileName;
  808.   { Return modified or non-modified string }
  809.   ForceTrailingBackslash := TempString;
  810. end;
  811.  
  812. { This function makes sure a non-root dir has no trailing \ }
  813. function TFileWorkBench.StripNonRootTrailingBackSlash(
  814.           const TheFileName : String ) : String;
  815. var TempString : String ; { Used to hold function result }
  816. begin
  817.   { Default is no change }
  818.   TempString := TheFileName;
  819.   { If not root then }
  820.   if Length( TheFileName ) > 3 then
  821.   begin
  822.     { If has a trailing backslash remove it }
  823.     if TheFileName[ Length( TheFileName )] = '\' then
  824.     begin
  825.       TempString := Copy( TheFileName , 1 ,
  826.        Length( TheFileName ) - 1 );
  827.     end;
  828.   end;
  829.   { Export the final result }
  830.   StripNonRootTrailingBackSlash := TempString;
  831. end;
  832.  
  833. { This gets the next selected listbox item }
  834. function TIconFileListBox.GetNextSelection( SourceDirectory : String;
  835.           var CurrentItem : Integer ): String;
  836. var TheResult : String;  { Internal storage }
  837.     finished  : boolean; { Loop flag        }
  838. begin
  839.   { If out of items to check signal and exit }
  840.   if CurrentItem > Items.Count then TheResult := '' else
  841.   begin
  842.     { Otherwise scan from current position till match or end }
  843.     finished := false;
  844.     while not finished do
  845.     begin
  846.       { Check against selected property }
  847.       if Selected[ CurrentItem - 1 ] then
  848.       begin
  849.         { If selected then return it and abort loop }
  850.         TheResult := SourceDirectory + Items[ CurrentItem - 1 ];
  851.         finished := true;
  852.         { Increment current position }
  853.         CurrentItem := CurrentItem + 1;
  854.      end
  855.       else
  856.       begin
  857.         { Increment current position }
  858.         CurrentItem := CurrentItem + 1;
  859.         { Otherwise check for end of data and abort if out of entries }
  860.         if CurrentItem > Items.Count then
  861.         begin
  862.           TheResult := '';
  863.           finished := true;
  864.         end;
  865.       end;
  866.     end;
  867.   end;
  868.   { Return stored result }
  869.   GetNextSelection := TheResult;
  870. end;
  871.  
  872. { Create method for FIP                                }
  873. constructor TFileIconPanel.Create( AOwner : TComponent );
  874. begin
  875.   { call inherited -- VITAL! }
  876.   inherited Create( AOwner );
  877.   { create icon and label components, making self owner/displayer }
  878.   FTheIcon := TIcon.Create;
  879.   FTheLabel := TLabel.Create( Self );
  880.   FThelabel.Parent := Self;
  881.   { Set own and labels mouse methods to stored methods }
  882.   OnClick := TheClick;
  883.   OnDblClick := TheDblClick;
  884.   FTheLabel.OnClick := TheClick;
  885.   FTheLabel.OnDblClick := TheDblClick;
  886.   { Set alignment and autosize properties of the label }
  887.   FTheLabel.Autosize := false;
  888.   FTheLabel.Alignment := taCenter;
  889.   { Set selected to false }
  890.   Selected := false;
  891. end;
  892.  
  893. { Initialization method for FIP                                         }
  894. procedure TFileIconPanel.Initialize( PanelX              ,
  895.                                      PanelY              ,
  896.                                      PanelWidth          ,
  897.                                      PanelHeight         ,
  898.                                      PanelBevelWidth     ,
  899.                                      LabelFontSize         : Integer;
  900.                                      PanelColor          ,
  901.                                      PanelHighlightColor ,
  902.                                      PanelShadowColor    ,
  903.                                      LabelTextColor        : TColor;
  904.                                      TheFilename         ,
  905.                                      LabelFontName         : String;
  906.                                      LabelFontStyle        : TFontStyles;
  907.                                      ExtraData             : Integer );
  908.  
  909. var TheLabelHeight ,             { Holder for label pixel height }
  910.     TheLabelWidth    : Integer;  { Holder for label pixel width  }
  911.     TheOtherPChar    : PChar;    { Windows ASCIIZ string         }
  912. begin
  913.   { Set the basic properties based on imported parameters }
  914.   Left := PanelX;
  915.   Top := PanelY;
  916.   Width := PanelWidth;
  917.   Height := PanelHeight;
  918.   Color := PanelColor;
  919.   BevelWidth := PanelBevelWidth;
  920.   FHighlightColor := PanelHighlightColor;
  921.   FShadowColor := PanelShadowColor;
  922.   FTheName := TheFilename;
  923.   { If the ExtraData field is non-0 then a drive is being sent in }
  924.   if ExtraData <> 0 then
  925.   begin
  926.     { Use the data field value to determine which icon to get from RES file }
  927.     case ExtraData of
  928.       1 : begin
  929.             GetMem( TheOtherPChar , 255 );
  930.             StrPCopy( TheOtherPChar , 'FLOPPY35' );
  931.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  932.             FreeMem( TheOtherPChar , 255 );
  933.           end;
  934.       2 : begin
  935.             GetMem( TheOtherPChar , 255 );
  936.             StrPCopy( TheOtherPChar , 'FIXEDHD' );
  937.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  938.             FreeMem( TheOtherPChar , 255 );
  939.           end;
  940.       3 : begin
  941.             GetMem( TheOtherPChar , 255 );
  942.             StrPCopy( TheOtherPChar , 'NETWORKHD' );
  943.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  944.             FreeMem( TheOtherPChar , 255 );
  945.           end;
  946.       4 : begin
  947.             GetMem( TheOtherPChar , 255 );
  948.             StrPCopy( TheOtherPChar , 'CDROM' );
  949.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  950.             FreeMem( TheOtherPChar , 255 );
  951.           end;
  952.       5 : begin
  953.             GetMem( TheOtherPChar , 255 );
  954.             StrPCopy( TheOtherPChar , 'RAM' );
  955.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  956.             FreeMem( TheOtherPChar , 255 );
  957.           end;
  958.     end;
  959.     { The FileNme property is already set up for the caption; use directly }
  960.     FTheLabel.Caption := TheFilename;
  961.     { Set up the hint for later use (make sure to set ShowHint) }
  962.     Hint := 'Change to ' + TheFileName;
  963.     ShowHint := true;
  964.     { Set up all imported label properties and center it for drawing }
  965.     with FTheLabel do
  966.     begin
  967.       Font.Name := LabelFontName;
  968.       Font.Size := LabelFontSize;
  969.       Font.Style := LabelFontStyle;
  970.       Font.Color := LabelTextColor;
  971.       Canvas.Brush.Color := PanelColor;
  972.       Canvas.Font := Font;
  973.       TheLabelHeight := Canvas.Textheight( Caption ) + 4;
  974.       TheLabelWidth := Canvas.Textwidth( Caption ) + 4;
  975.       Left := (( Self.Width - TheLabelWidth ) div 2 ) + 1;
  976.       Top := ((( Round( Self.Height * 0.25 ) - 6 ) - TheLabelHeight) div 2) + 1;
  977.       Top := Top + Round( Self.Height * 0.75 );
  978.       Height := TheLabelHeight;
  979.       Width := TheLabelWidth;
  980.     end;
  981.   end
  982.   else
  983.   begin
  984.     { A file or directory has been sent in; use GetIconForFile to obtain an }
  985.     { icon either from the file, its owner, or a RES file default.          }
  986.     GetIconForFile( FTheName , FTheIcon );
  987.     { Check for the Backup caption and set it specially }
  988.     if ExtractfileName( FThename ) = '..' then
  989.     begin
  990.       FTheLabel.Caption := '..';
  991.       Hint := 'Up One Level';
  992.     end
  993.     else
  994.     begin
  995.       { Otherwise just get the filename for the label caption }
  996.       { And the full path for the hint (used later.)          }
  997.       FTheLabel.caption := ExtractFileName( UpperCase( FTheName ));
  998.       Hint := FTheName;
  999.     end;
  1000.     { Activate showhint so hints are seen }
  1001.     ShowHint := true;
  1002.     { Set label properties with imported values and center for display }
  1003.     with FTheLabel do
  1004.     begin
  1005.       Font.Name := LabelFontName;
  1006.       Font.Size := LabelFontSize;
  1007.       Font.Style := LabelFontStyle;
  1008.       Font.Color := LabelTextColor;
  1009.       Canvas.Brush.Color := PanelColor;
  1010.       Canvas.Font := Font;
  1011.       TheLabelHeight := Canvas.Textheight( Caption ) + 4;
  1012.       TheLabelWidth := Canvas.Textwidth( Caption ) + 4;
  1013.       Left := (( Self.Width - TheLabelWidth ) div 2 ) + 1;
  1014.       Top := ((( Round( Self.Height * 0.25 ) - 6 ) - TheLabelHeight) div 2) + 1;
  1015.       Top := Top + Round( Self.Height * 0.75 );
  1016.       Height := TheLabelHeight;
  1017.       Width := TheLabelWidth;
  1018.     end;
  1019.   end;
  1020. end;
  1021.  
  1022. { Destroy method for FIP }
  1023. destructor TFileIconPanel.Destroy;
  1024. begin
  1025.   { free component resources }
  1026.   FTheIcon.Free;
  1027.   FTheLabel.Free;
  1028.   { call inherited -- VITAL! }
  1029.   inherited Destroy;
  1030. end;
  1031.  
  1032. { TheClick method for FIP; used for event responses }
  1033. procedure TFileIconPanel.TheClick( Sender : TObject );
  1034. begin
  1035.   { Currently ignore drive clicks }
  1036.   if Pos( 'DRIVE' , FTheName ) > 0 then exit;
  1037.   { Flip status of bevels }
  1038.   if BevelOuter = bvRaised then BevelOuter := bvLowered else
  1039.    BevelOuter := bvRaised;
  1040.   { Flip selected variable }
  1041.   Selected := not Selected;
  1042.   { Set redisplay }
  1043.   Invalidate;
  1044. end;
  1045.  
  1046. { TheDblClick method for FIP; used for event responses }
  1047. procedure TFileIconPanel.TheDblClick( Sender : TObject );
  1048. var CurrentDirectory : String;    { Use to store dirs }
  1049.     TheDrive         : String;    { Get drive letter  }
  1050.     WhichDrive       : Integer;   { Get drive number  }
  1051.     ErrorCheck       : Integer;
  1052.     TheFWB           : TFileWorkBench;
  1053. begin
  1054.   { Create FileWorkBench for later use }
  1055.   TheFWB := TFileWorkBench.Create( Self );
  1056.   { Check for label or FIP sender }
  1057.   if Sender is TFileIconPanel then
  1058.   begin
  1059.     if FTheLabel.Caption = '..' then
  1060.     begin { deal with backup request }
  1061.       { Change to new directory }
  1062.       TheFWB.ChangeTheDirectory( '..' );
  1063.       { Call special method due to SendMessage problem! }
  1064.       TFileIconPanelScrollBox( Parent ).Update;
  1065.     end
  1066.     else
  1067.     begin
  1068.       { Check for DRIVE id in name }
  1069.       if Pos( 'DRIVE' , FTheName ) <> 0 then
  1070.       begin { Double Click on a Drive Icon }
  1071.         { Pull out the letter from name }
  1072.         TheDrive := Copy( FtheName , 7 , 1 );
  1073.         { Convert it to a number }
  1074.         WhichDrive := ( Ord( TheDrive[ 1 ] ) - Ord( 'A' )) + 1;
  1075.         TheFWB.ChangeTheDriveAndDirectory( WhichDrive );
  1076.         { Call special method due to SendMessage problem! }
  1077.         TFileIconPanelScrollBox( Parent ).Update;
  1078.       end
  1079.       else
  1080.       begin { Double click on a dir/file icon }
  1081.         if (( FileGetAttr( FTheName ) and faDirectory ) = faDirectory ) then
  1082.         begin { A directory, change to it }
  1083.           { Since full path in name, simply change to it! }
  1084.           TheFWB.ChangeTheDirectory( FTheName );
  1085.           { Call special method due to SendMessage problem! }
  1086.           TFileIconPanelScrollBox( Parent ).Update;
  1087.         end
  1088.         else
  1089.         begin { A file; attempt to shellexecute it }
  1090.           { Call shellexec as a wrapper around ShellExecute API call }
  1091.           { False indicates failure, signal error                    }
  1092.           if not ShellExec( FTheName , '' , '', false , SW_SHOWNORMAL , false )
  1093.            then MessageDlg('Could not Shell out to ' + FTheName , mtError,
  1094.             [mbOK], 0);
  1095.         end;
  1096.       end;
  1097.     end;
  1098.   end
  1099.   else
  1100.   begin
  1101.     with Sender as TLabel do
  1102.     begin
  1103.       if Caption = '..' then
  1104.       begin { Deal with backup request }
  1105.         { Change to new directory }
  1106.         TheFWB.ChangeTheDirectory( '..' );
  1107.         { Call special method due to SendMessage problem! }
  1108.         TFileIconPanelScrollBox( Parent ).Update;
  1109.       end
  1110.       else
  1111.       begin
  1112.         with Parent as TFileIconPanel do
  1113.         begin
  1114.           { Check for DRIVE id in name }
  1115.           if Pos( 'DRIVE' , FTheName ) <> 0 then
  1116.           begin { Double Click on a Drive Icon }
  1117.             { Pull out the letter from name }
  1118.             TheDrive := Copy( FtheName , 7 , 1 );
  1119.             { Convert it to a number }
  1120.             WhichDrive := ( Ord( TheDrive[ 1 ] ) - Ord( 'A' )) + 1;
  1121.             { Call the method to change to default dir on new drive }
  1122.             TheFWB.ChangeTheDriveAndDirectory( WhichDrive );
  1123.             { Call special method due to SendMessage problem! }
  1124.             TFileIconPanelScrollBox( Parent ).Update;
  1125.           end
  1126.           else
  1127.           begin { Double click on a dir/file icon }
  1128.             if (( FileGetAttr( FTheName ) and faDirectory ) = faDirectory ) then
  1129.             begin { A directory, change to it }
  1130.               { Since full path in name, simply change to it! }
  1131.               TheFWB.ChangeTheDirectory( FTheName );
  1132.               { Call special method due to SendMessage problem! }
  1133.               TFileIconPanelScrollBox( Parent ).Update;
  1134.             end
  1135.             else
  1136.             begin { A file; attempt to shellexecute it }
  1137.               { Call shellexec as a wrapper around ShellExecute API call }
  1138.               { False indicates failure, signal error                    }
  1139.               if not ShellExec( FTheName , '' , '', false , SW_SHOWNORMAL ,
  1140.                false ) then MessageDlg('Could not Shell out to ' + FTheName ,
  1141.                 mtError, [mbOK], 0);
  1142.             end;
  1143.           end;
  1144.         end;
  1145.       end;
  1146.     end;
  1147.   end;
  1148.   TheFWB.Free; { This prevents resource leak }
  1149. end;
  1150.  
  1151. { Paint method for FIP; overrides normal paint }
  1152. procedure TFileIconPanel.Paint;
  1153. var
  1154.   TheOtherRect   : TRect;   { Holds clientrect   }
  1155.   TopColor     ,            { Holds bright color }
  1156.   BottomColor    : TColor;  { Holds dark color   }
  1157.  
  1158. { These methods are from Borland Intl., copyright 1995 }
  1159. procedure Frame3D(    Canvas       : TCanvas;
  1160.                   var TheRect      : TRect;
  1161.                       TopColor   ,
  1162.                       BottomColor  : TColor;
  1163.                       Width        : Integer );
  1164.  
  1165. procedure DoRect;
  1166. var
  1167.   TopRight, BottomLeft: TPoint;
  1168. begin
  1169.   with Canvas, TheRect do
  1170.   begin
  1171.     TopRight.X := Right;
  1172.     TopRight.Y := Top;
  1173.     BottomLeft.X := Left;
  1174.     BottomLeft.Y := Bottom;
  1175.     Pen.Color := TopColor;
  1176.     PolyLine([BottomLeft, TopLeft, TopRight]);
  1177.     Pen.Color := BottomColor;
  1178.     Dec(BottomLeft.X);
  1179.     PolyLine([TopRight, BottomRight, BottomLeft]);
  1180.   end;
  1181. end;
  1182.  
  1183. begin
  1184.   Canvas.Pen.Width := 1;
  1185.   Dec(TheRect.Bottom); Dec(TheRect.Right);
  1186.   while Width > 0 do
  1187.   begin
  1188.     Dec(Width);
  1189.     DoRect;
  1190.     InflateRect(TheRect, -1, -1);
  1191.   end;
  1192.   Inc(TheRect.Bottom); Inc(TheRect.Right);
  1193. end;
  1194.  
  1195. procedure AdjustColors(Bevel: TPanelBevel);
  1196. begin
  1197.   TopColor := FHighlightColor;
  1198.   if Bevel = bvLowered then TopColor := FShadowColor;
  1199.   BottomColor := FShadowColor;
  1200.   if Bevel = bvLowered then BottomColor := FHighlightColor;
  1201. end;
  1202.  
  1203. { Custom code begins here }
  1204. begin
  1205.   { Get the rectangle of the control with API/method call }
  1206.   TheOtherRect := GetClientRect;
  1207.   { draw basic rectangle with basic color }
  1208.   with Canvas do
  1209.   begin
  1210.     Brush.Color := Color;
  1211.     FillRect(TheOtherRect);
  1212.   end;
  1213.   { Set up for top "icon" frame  and draw it with frame3d }
  1214.   TheOtherRect.Right := Width;
  1215.   TheOtherRect.Bottom := Round( Height * 0.75 ) - 6 ;
  1216.   if BevelOuter <> bvNone then
  1217.   begin
  1218.     AdjustColors(BevelOuter);
  1219.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1220.   end;
  1221.   Frame3D(Canvas, TheOtherRect, Color, Color, BorderWidth);
  1222.   if BevelInner <> bvNone then
  1223.   begin
  1224.     AdjustColors(BevelInner);
  1225.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1226.   end;
  1227.   { Do the same for the lower "label" frame }
  1228.   TheOtherRect.Top := Round( Height * 0.75 ) - 5;
  1229.   TheOtherRect.Left := 0;
  1230.   TheOtherRect.Bottom := Height;
  1231.   TheOtherRect.Right := Width;
  1232.   if BevelOuter <> bvNone then
  1233.   begin
  1234.     AdjustColors(BevelOuter);
  1235.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1236.   end;
  1237.   Frame3D(Canvas, TheOtherRect, Color, Color, BorderWidth);
  1238.   if BevelInner <> bvNone then
  1239.   begin
  1240.     AdjustColors(BevelInner);
  1241.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1242.   end;
  1243.   { Then draw the icon using canvas draw method }
  1244.   Canvas.Draw( (( Width - 32 ) div 2 ) + 1 ,
  1245.   ((( Round( Height * 0.75 ) - 6 ) - 32 ) div 2 ) + 1 , FTheIcon );
  1246. end;
  1247.  
  1248. { This procedure clears a scrollbox of all FileIconPanels }
  1249. procedure TFileIconPanelScrollbox.ClearTheFIPs;
  1250. var Counter_1 : Integer;
  1251.     TheComponent : TComponent;
  1252. begin
  1253.   { Note that must use while loop since component count continually }
  1254.   { decreases as removes are made!                                  }
  1255.   while ComponentCount > 0 do
  1256.   begin
  1257.     { Save the component as a generic TComponent }
  1258.     TheComponent := Components[ 0 ];
  1259.     { Call removecomponent to pull it out of the owner list for sb }
  1260.     { This avoids GPF when freeing the sb.                         }
  1261.     RemoveComponent( Components[ 0 ]);
  1262.     { Typecast the pointer and free it to release memory and res. }
  1263.     TFileIconPanel( TheComponent ).Free;
  1264.   end;
  1265. end;
  1266.  
  1267. { This procedure scans for drives and obtains their type and creates file }
  1268. { icon panels to represent them.                                          }
  1269. procedure TFileIconPanelScrollBox.AddDriveIcons( var XCounter ,
  1270.            YCounter : Integer );
  1271. type
  1272.   { This if from filectrl unit; reproduce here for completeness }
  1273.   TDriveType = (dtUnknown, dtNoDrive, dtFloppy, dtFixed, dtNetwork, dtCDROM,
  1274.                 dtRAM);
  1275. var
  1276.   DriveNum        : Integer;         { Used to get next drive via DOS fn   }
  1277.   IconType        : Integer;         { Used to hold icon type (defacto dt) }
  1278.   DriveChar       : Char;            { Used to hold drive letter           }
  1279.   DriveType       : TDriveType;      { Used for set-valued drive type      }
  1280.   Finished        : Boolean;         { Loop flag                           }
  1281.   TheFIP          : TFileIconPanel;  { Generic FileIconPanel variable      }
  1282.   ButtonColor   ,                    { Main panel color                    }
  1283.   ButtonHLColor ,                    { Bright panel color                  }
  1284.   ButtonSColor  ,                    { Dark panel color                    }
  1285.   Textcolor       : TColor;          { Label text color                    }
  1286.  
  1287. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  1288. { Check whether drive is a CD-ROM.  Returns True if MSCDEX is installed }
  1289. {  and the drive is using a CD driver                                   }
  1290.  
  1291. function IsCDROM(DriveNum: Integer): Boolean; assembler;
  1292. asm
  1293.   MOV   AX,1500h { look for MSCDEX }
  1294.   XOR   BX,BX
  1295.   INT   2fh
  1296.   OR    BX,BX
  1297.   JZ    @Finish
  1298.   MOV   AX,150Bh { check for using CD driver }
  1299.   MOV   CX,DriveNum
  1300.   INT   2fh
  1301.   OR    AX,AX
  1302.   @Finish:
  1303. end;
  1304.  
  1305. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  1306. { Check whether drive is a RAM drive.                                   }
  1307. function IsRAMDrive(DriveNum: Integer): Boolean; assembler;
  1308. var
  1309.   TempResult: Boolean;
  1310. asm
  1311.   MOV   TempResult,False
  1312.   PUSH  DS
  1313.   MOV   BX,SS
  1314.   MOV   DS,BX
  1315.   SUB   SP,0200h
  1316.   MOV   BX,SP
  1317.   MOV   AX,DriveNum
  1318.   MOV   CX,1
  1319.   XOR   DX,DX
  1320.   INT   25h  { read boot sector }
  1321.   ADD   SP,2
  1322.   JC    @ItsNot
  1323.   MOV   BX,SP
  1324.   CMP   BYTE PTR SS:[BX+15h],0F8h  { reverify fixed disk }
  1325.   JNE   @ItsNot
  1326.   CMP   BYTE PTR SS:[BX+10h],1  { check for single FAT }
  1327.   JNE   @ItsNot
  1328.   MOV   TempResult,True
  1329.   @ItsNot:
  1330.   ADD   SP,0200h
  1331.   POP   DS
  1332.   MOV   AL, TempResult
  1333. end;
  1334.  
  1335. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  1336. { Finds the type of a drive letter.                                     }
  1337. function FindDriveType(DriveNum: Integer): TDriveType;
  1338. begin
  1339.   Result := TDriveType(GetDriveType(DriveNum));
  1340.   if (Result = dtFixed) or (Result = dtNetwork) then
  1341.   begin
  1342.     if IsCDROM(DriveNum) then Result := dtCDROM
  1343.     else if (Result = dtFixed) then
  1344.     begin
  1345.         { do not check for RAMDrive under Windows NT }
  1346.       if ((GetWinFlags and $4000) = 0) and IsRAMDrive(DriveNum) then
  1347.         Result := dtRAM;
  1348.     end;
  1349.   end;
  1350. end;
  1351.  
  1352. begin
  1353.   { Set the button colors to an aquamarine color scheme for drives }
  1354.   ButtonColor := clTeal;
  1355.   ButtonHLColor := clAqua;
  1356.   ButtonSColor := clNavy;
  1357.   TextColor := clblack;
  1358.   { Set initial variables before looping for all drives }
  1359.   finished := false;
  1360.   DriveNum := 0;
  1361.   while not finished do
  1362.   begin
  1363.     { Start with no drive found }
  1364.     IconType := 0;
  1365.     { Call the Borland method to get the drive info }
  1366.     DriveType := FindDriveType(DriveNum);
  1367.     { Set its letter and make it uppercase }
  1368.     DriveChar := Chr(DriveNum + ord('a'));
  1369.     DriveChar := Upcase(DriveChar);
  1370.     { Assign an icon based on the drive type; if no drive exists type is nil }
  1371.     case DriveType of
  1372.       dtFloppy  : IconType := 1;
  1373.       dtFixed   : IconType := 2;
  1374.       dtNetwork : IconType := 3;
  1375.       dtCDROM   : IconType := 4;
  1376.       dtRAM     : IconType := 5;
  1377.     end;
  1378.     { Set to check next drive letter }
  1379.     DriveNum := DriveNum + 1;
  1380.     { But if no match then out of drives so set exit flag }
  1381.     if IconType = 0 then finished := true;
  1382.     { If drive was valid then set up the new FileIconPanel on the imported }
  1383.     { Scrollbox                                                            }
  1384.     if not finished then
  1385.     begin
  1386.       { Create the FileIconPanel and set its parent for memory mgmt and display}
  1387.       TheFIP := TFileIconPanel.Create( Self );
  1388.       TheFIP.Parent := Self;
  1389.       { Call its initialize method with imported position values and the   }
  1390.       { preset color scheme, a drive caption, and a minimum font. Note the }
  1391.       { setting of the ExtraData field to non-zero; this signals a drive   }
  1392.       { rather than a file being sent in.                                  }
  1393.       TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1394.        (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1395.         7 , ButtonColor, ButtonHLColor,
  1396.        ButtonSColor , TextColor , 'DRIVE ' + DriveChar + ':' , 'MS Serif' , [] ,
  1397.        IconType );
  1398.       { Increment the column counter; if it exceeds max move to new row      }
  1399.       { Note that these are 'var' parameters and will export final position. }
  1400.       XCounter := XCounter + 1;
  1401.       if XCounter > MaxIconsInARow then
  1402.       begin
  1403.         XCounter := 1;
  1404.         YCounter := YCounter + 1;
  1405.       end;
  1406.     end;
  1407.   end;
  1408. end;
  1409.  
  1410. { This procedure assigns colors to FIP's based on file attributes }
  1411. procedure TFileIconPanelScrollBox.GetColorsForFileIcon( TheFile : String;
  1412.            var BC , HC , SC , TC : TColor );
  1413. var AmADir      ,             { Booleans hold file attribs }
  1414.     AmAnArchive ,
  1415.     AmAVolumeId ,
  1416.     AmHidden    ,
  1417.     AmReadOnly  ,
  1418.     AmSystem      : Boolean;
  1419. begin
  1420.   { Make the call to internal fileworkbench to set attributes }
  1421.   TheFWB.GetFileAttributes( TheFile , AmADir , AmAnArchive , AmAVolumeId ,
  1422.    AmHidden , AmReadOnly , AmSystem );
  1423.   { Volume ID has no subtypes }
  1424.   if AmAVolumeID then
  1425.   begin
  1426.     BC := clOlive;
  1427.     HC := clYellow;
  1428.     SC := clBlack;
  1429.     TC := clWhite;
  1430.     exit;
  1431.   end;
  1432.   { Check all directory combinations }
  1433.   if AmADir then
  1434.   begin
  1435.     BC := clNavy;
  1436.     HC := clBlue;
  1437.     SC := clBlack;
  1438.     TC := clWhite;
  1439.     if AmHidden then
  1440.     begin
  1441.       if AmReadOnly then
  1442.       begin
  1443.         if AmSystem then
  1444.         begin { One HECK of a file! }
  1445.           BC := clBlack;
  1446.           HC := clSilver;
  1447.           SC := clGray;
  1448.           TC := clWhite;
  1449.         end
  1450.         else
  1451.         begin { Dir,RO,Hid }
  1452.           BC := clMaroon;
  1453.           HC := clFuchsia;
  1454.           SC := clGreen;
  1455.           TC := clWhite;
  1456.         end;
  1457.       end
  1458.       else
  1459.       begin { Dir,Hid }
  1460.         BC := clPurple;
  1461.         HC := clFuchsia;
  1462.         SC := clBlack;
  1463.         TC := clWhite;
  1464.       end;
  1465.     end
  1466.     else
  1467.     begin
  1468.       if AmReadOnly then
  1469.       begin
  1470.         if AmSystem then
  1471.         begin { Dir,RO,Sys }
  1472.           BC := clMaroon;
  1473.           HC := clLime;
  1474.           SC := clGreen;
  1475.           TC := clWhite;
  1476.         end
  1477.         else
  1478.         begin { Dir,RO }
  1479.           BC := clGreen;
  1480.           HC := clLime;
  1481.           SC := clBlack;
  1482.           TC := clWhite;
  1483.         end;
  1484.       end
  1485.       else
  1486.       begin
  1487.         if AmSystem then
  1488.         begin { Dir,Sys }
  1489.           BC := clMaroon;
  1490.           HC := clRed;
  1491.           SC := clBlack;
  1492.           TC := clWhite;
  1493.         end;
  1494.       end;
  1495.     end;
  1496.   end
  1497.   else { Archive Only; check all combinations }
  1498.   begin
  1499.     BC := clSilver;
  1500.     HC := clWhite;
  1501.     SC := clGray;
  1502.     TC := clBlack;
  1503.     if AmHidden then
  1504.     begin
  1505.       if AmReadOnly then
  1506.       begin
  1507.         if AmSystem then
  1508.         begin { Hid,RO,Sys }
  1509.           BC := clRed;
  1510.           HC := clLime;
  1511.           SC := clPurple;
  1512.           TC := clBlack;
  1513.         end
  1514.         else
  1515.         begin { RO,Hid }
  1516.           BC := clLime;
  1517.           HC := clFuchsia;
  1518.           SC := clMaroon;
  1519.           TC := clBlack;
  1520.         end;
  1521.       end
  1522.       else
  1523.       begin { Hid }
  1524.         BC := clFuchsia;
  1525.         HC := clWhite;
  1526.         SC := clPurple;
  1527.         TC := clBlack;
  1528.       end;
  1529.     end
  1530.     else
  1531.     begin
  1532.       if AmReadOnly then
  1533.       begin
  1534.         if AmSystem then
  1535.         begin { RO,Sys }
  1536.           BC := clRed;
  1537.           HC := clLime;
  1538.           SC := clMaroon;
  1539.           TC := clBlack;
  1540.         end
  1541.         else
  1542.         begin { RO }
  1543.           BC := clLime;
  1544.           HC := clWhite;
  1545.           SC := clGreen;
  1546.           TC := clBlack;
  1547.         end;
  1548.       end
  1549.       else
  1550.       begin
  1551.         if AmSystem then
  1552.         begin { System }
  1553.           BC := clRed;
  1554.           HC := clWhite;
  1555.           SC := clMaroon;
  1556.           TC := clBlack;
  1557.         end;
  1558.       end;
  1559.     end;
  1560.   end;
  1561. end;
  1562.  
  1563. { This procedure gets all icons for an given directory, including drives and }
  1564. { standard subdirectories. It does not get special combinations or h/ro/sys  }
  1565. procedure TFileIconPanelScrollbox.GetIconsForEntireDirectory(
  1566.             TargetPath  : String );
  1567. var Finished        : Boolean;         { Loop flag              }
  1568.     TheSR           : TSearchRec;      { Searchrecord for FF/FN }
  1569.     TheResult       : Integer;         { return variable        }
  1570.     TempPath        : String;          { path for FF/FN         }
  1571.     TheFIP          : TFileIconPanel;  { generic FIP holder     }
  1572.     RowCounter    ,                    { position in row of FIP }
  1573.     ColumnCounter   : Integer;         { position in col of FIP }
  1574.     ButtonColor   ,                    { main panel color       }
  1575.     ButtonHLColor ,                    { bright panel color     }
  1576.     ButtonSColor  ,                    { dark panel color       }
  1577.     Textcolor       : TColor;          { label text color       }
  1578.     IsADir ,                           { Variable for file attr }
  1579.     IsAnArchive ,
  1580.     IsAVolumeID,
  1581.     IsAReadOnlyFile,
  1582.     IsAHiddenFile ,
  1583.     IsASystemFile     : Boolean;
  1584.     MaxTextLength     : Integer;       { Used to safely set size}
  1585. begin
  1586.   { hide during refresh }
  1587.   Visible := false;
  1588.   { Delete the current set, if any }
  1589.   ClearTheFIPs;
  1590.   { Get the icon sizes }
  1591.   TheFIP := TFileIconPanel.Create( Self );
  1592.   TheFIP.Parent := Self;
  1593.   TheFIP.FTheLabel.Canvas.Font.Name := 'MS Serif';
  1594.   TheFIP.FTheLabel.Canvas.Font.Size := 7;
  1595.   MaxTextLength := TheFIP.FTheLabel.Canvas.TextWidth( 'COMMAND.COM' );
  1596.   TheFIP.Free;
  1597.   TheIconSize := MaxTextLength + 13;
  1598.   TheIconSpacing := TheIconSize + 5;
  1599.   { Set up maximum icons per row based on screen size }
  1600.   MaxIconsInARow := ( Screen.Width div TheIconSpacing );
  1601.   { Set up the position counters }
  1602.   RowCounter := 1;
  1603.   ColumnCounter := 1;
  1604.   { Get the drives for the current machine }
  1605.   AddDriveIcons( ColumnCounter , RowCounter  );
  1606.   { Set up the initial variables }
  1607.   Finished := false;
  1608.   TempPath := TargetPath + '*.*';
  1609.   { Make the call to FindFirst set to get any file; will return '.' }
  1610.   { so discard it.                                                  }
  1611.   TheResult := FindFirst( TempPath , faAnyFile , TheSR );
  1612.   { loop through all files in the directory and look for directories }
  1613.   while not Finished do
  1614.   begin
  1615.     { Make call to FindNext, using only SearchRecord from FindFirst }
  1616.     TheResult := FindNext( TheSR );
  1617.     { A -1 result means no more files so exit }
  1618.     if TheResult < 0 then finished := true else
  1619.     begin
  1620.       { Otherwise check for a directory attribute }
  1621.       if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory ) =
  1622.        faDirectory ) then
  1623.       begin
  1624.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1625.          ButtonHLColor , ButtonSColor , TextColor );
  1626.         { If found create a new FileIconPanel on the imported scrollbox }
  1627.         { Note sending 0 ExtraData parameter to indicate file not drive }
  1628.         TheFIP := TFileIconPanel.Create( Self );
  1629.         TheFIP.Parent := Self;
  1630.         TheFIP.Initialize((( ColumnCounter - 1 ) * TheIconSpacing ),
  1631.          (( RowCounter - 1  ) * TheIconSpacing ) , TheIconSize, TheIconSize ,
  1632.           3 , 7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1633.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1634.         { Increment column counter and move to new row if past limit }
  1635.         ColumnCounter := ColumnCounter + 1;
  1636.         if ColumnCounter > MaxIconsInARow then
  1637.         begin
  1638.           ColumnCounter := 1;
  1639.           RowCounter := RowCounter + 1;
  1640.         end;
  1641.       end;
  1642.     end;
  1643.   end;
  1644.   { Call FindClose for Windows NT/Windows 95 compatibility }
  1645.   FindClose( TheSR );
  1646.   { Set up new initialization variables }
  1647.   Finished := false;
  1648.   TempPath := TargetPath + '*.*';
  1649.   { Make needed call to FindFirst and discard '.' }
  1650.   TheResult := FindFirst( TempPath , faAnyFile , TheSR );
  1651.   while not Finished do
  1652.   begin
  1653.     { Loop through file again, this time getting only archive files }
  1654.     TheResult := FindNext( TheSR );
  1655.     { Result of -1 indicates no more files }
  1656.     if TheResult < 0 then Finished := true else
  1657.     begin
  1658.       { If faArchive file then add new FileIconPanel }
  1659.       TheFWB.GetFileAttributes(( Targetpath + TheSR.Name ) , IsADir ,
  1660.        IsAnArchive , IsAVolumeId , IsAHiddenFile , IsAReadOnlyFile ,
  1661.         IsASystemFile );
  1662.       if (( IsAnArchive ) and ( not IsADir )) then
  1663.       begin
  1664.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1665.          ButtonHLColor , ButtonSColor , TextColor );
  1666.         { Initialize new FileIconPanel and call initialize, sending 0 ED }
  1667.         TheFIP := TFileIconPanel.Create( Self );
  1668.         TheFIP.Parent := Self;
  1669.         TheFIP.Initialize((( ColumnCounter - 1 ) * TheIconSpacing ),
  1670.          (( RowCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize ,
  1671.           3 , 7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1672.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1673.         { Increment column counter and if needed row counter }
  1674.         ColumnCounter := ColumnCounter + 1;
  1675.         if ColumnCounter > MaxIconsInARow then
  1676.         begin
  1677.           ColumnCounter := 1;
  1678.           RowCounter := RowCounter + 1;
  1679.         end;
  1680.       end;
  1681.     end;
  1682.   end;
  1683.   { Call findclose for w95 and exit }
  1684.   FindClose( TheSR );
  1685.   { Reset to visible }
  1686.   Visible := true;
  1687. end;
  1688.  
  1689. { Update method for FIPscrollbox }
  1690. procedure TFileIconPanelScrollBox.Update;
  1691. begin
  1692.   IconsNeedRefreshing := true;
  1693.   { Force a repaint }
  1694.   InvalidateRect( TheStoredHandle , nil , true );
  1695. end;
  1696.  
  1697. { Create method for FIPScrollbox }
  1698. constructor TFileIconPanelScrollBox.Create( AOwner : TComponent );
  1699. begin
  1700.   inherited Create( AOwner );
  1701.   TheFWB := TFileWorkBench.Create( Self );
  1702. end;
  1703.  
  1704. { This function returns the next selected file's name }
  1705. function TFileIconPanelScrollBox.GetNextSelection( SourceDirectory : String;
  1706.                            var CurrentItem : Integer ) : String;
  1707. var TheResult    : String;      { Holds result of function }
  1708.     TheComponent : TComponent;  { Used for typecast        }
  1709.     finished     : boolean;     { Loop control variable    }
  1710.     TheComponentCount : Integer;
  1711. begin
  1712.   TheComponentCount := ComponentCount;
  1713.   { If past end of components exit with no result }
  1714.   if CurrentItem > TheComponentCount then TheResult := '' else
  1715.   begin
  1716.     { Set loop counter and run till find match or run out }
  1717.     finished := false;
  1718.     while not finished do
  1719.     begin
  1720.       { Pull component out of the list and check it }
  1721.       TheComponent := Components[ CurrentItem - 1 ];
  1722.       { Increment counter for later }
  1723.       CurrentItem := CurrentItem + 1;
  1724.       { Do the typecast with AS }
  1725.       with TheComponent as TFileIconPanel do
  1726.       begin
  1727.         { If its selected make sure OK }
  1728.         if Selected then
  1729.         begin
  1730.           { Don't accept backup for this level of operation }
  1731.           if FTheLabel.Caption <> '..' then
  1732.           begin
  1733.             { Otherwise return the name and abort the loop }
  1734.             TheResult := FTheName;
  1735.             finished := true;
  1736.           end;
  1737.         end
  1738.         else
  1739.         begin
  1740.           { Check to see if out of components }
  1741.           if CurrentItem > TheComponentCount then
  1742.           begin
  1743.             { If so signal error and abort }
  1744.             TheResult := '';
  1745.             finished := true;
  1746.           end;
  1747.         end;
  1748.       end;
  1749.     end;
  1750.   end;
  1751.   GetNextSelection := TheResult;
  1752. end;
  1753.  
  1754. { This procedure places a selection of files in the display based on wildcards }
  1755. procedure TFileIconPanelScrollBox.DisplayRecursiveSearchResults(
  1756.            TheStartingDirectory : String );
  1757. var XCounter ,
  1758.     YCounter   : Integer;
  1759.  
  1760. { This procedure does a recursive file search by first getting all matches (in-}
  1761. { cluding directories) and adding them to the list. Then it checks for ALL the }
  1762. { subdirectories and does the same trick on them til there are no more matches }
  1763. { and no more subdirectories, at which point it exits and recurses back up.    }
  1764. procedure RecursiveFileSearch( TheWorkingDirectory : String; var XCounter ,
  1765.                                YCounter : Integer );
  1766.  
  1767. { VITAL!!! These variables MUST be local for recursrion to work! }
  1768. var
  1769.     Finished        : Boolean;         { Loop flag              }
  1770.     TheSR           : TSearchRec;      { Searchrecord for FF/FN }
  1771.     TheResult       : Integer;         { return variable        }
  1772.     TargetPath ,
  1773.     FileMask   ,
  1774.     TheStoredWorkingDirectory ,
  1775.     ModifiedDirectory  : String;       { path for FF/FN         }
  1776.     TheFIP          : TFileIconPanel;  { generic FIP holder     }
  1777.     ButtonColor   ,                    { main panel color       }
  1778.     ButtonHLColor ,                    { bright panel color     }
  1779.     ButtonSColor  ,                    { dark panel color       }
  1780.     Textcolor       : TColor;          { label text color       }
  1781.  
  1782. begin
  1783.   { Set up the initial variables }
  1784.   Finished := false;
  1785.   TheStoredWorkingDirectory := TheWorkingDirectory;
  1786.   Targetpath := ExtractFilePath( TheWorkingDirectory );
  1787.   FileMask := ExtractFileName( TheWorkingDirectory );
  1788.   { Make the call to FindFirst set to get any file }
  1789.   TheResult := FindFirst( TheWorkingDirectory , faAnyFile , TheSR );
  1790.   if TheResult < 0 then finished := true;
  1791.   if (( TheSr.Name <> '.' ) and ( TheSr.Name <> '..' ) and ( TheResult >= 0 ))
  1792.   then begin
  1793.     if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory ) =
  1794.      faDirectory ) then
  1795.     begin { A directory }
  1796.       GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1797.        ButtonHLColor , ButtonSColor , TextColor );
  1798.       { If found create a new FileIconPanel on the imported scrollbox }
  1799.       { Note sending 0 ExtraData parameter to indicate file not drive }
  1800.       TheFIP := TFileIconPanel.Create( Self );
  1801.       TheFIP.Parent := Self;
  1802.       TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1803.        (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1804.         7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor , TargetPath
  1805.          + TheSr.Name , 'MS Serif' , [] , 0 );
  1806.       { Increment column counter and move to new row if past limit }
  1807.       XCounter := XCounter + 1;
  1808.       if XCounter > MaxIconsInARow then
  1809.       begin
  1810.         XCounter := 1;
  1811.         YCounter := YCounter + 1;
  1812.       end;
  1813.     end
  1814.     else
  1815.     begin { A File }
  1816.       { Set up the default color scheme for files }
  1817.       GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1818.        ButtonHLColor , ButtonSColor , TextColor );
  1819.       { If found create a new FileIconPanel on the imported scrollbox }
  1820.       { Note sending 0 ExtraData parameter to indicate file not drive }
  1821.       TheFIP := TFileIconPanel.Create( Self );
  1822.       TheFIP.Parent := Self;
  1823.       TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1824.        (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize, TheIconSize , 3 ,
  1825.         7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor , TargetPath
  1826.          + TheSr.Name , 'MS Serif' , [] , 0 );
  1827.       { Increment column counter and move to new row if past limit }
  1828.       XCounter := XCounter + 1;
  1829.       if XCounter > MaxIconsInARow then
  1830.       begin
  1831.         XCounter := 1;
  1832.         YCounter := YCounter + 1;
  1833.       end;
  1834.     end;
  1835.   end;
  1836.   { loop through all files in the directory and look for matches }
  1837.   while not Finished do
  1838.   begin
  1839.     { Make call to FindNext, using only SearchRecord from FindFirst }
  1840.     TheResult := FindNext( TheSR );
  1841.     { A -1 result means no more files so exit }
  1842.     if TheResult < 0 then finished := true else
  1843.     begin
  1844.       if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory ) =
  1845.        faDirectory ) then
  1846.       begin { A directory }
  1847.         { Set up the blue color scheme for directories }
  1848.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1849.          ButtonHLColor , ButtonSColor , TextColor );
  1850.         { If found create a new FileIconPanel on the imported scrollbox }
  1851.         { Note sending 0 ExtraData parameter to indicate file not drive }
  1852.         TheFIP := TFileIconPanel.Create( Self );
  1853.         TheFIP.Parent := Self;
  1854.         TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1855.          (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1856.            7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1857.             TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1858.         { Increment column counter and move to new row if past limit }
  1859.         XCounter := XCounter + 1;
  1860.         if XCounter > MaxIconsInARow then
  1861.         begin
  1862.           XCounter := 1;
  1863.           YCounter := YCounter + 1;
  1864.         end;
  1865.       end
  1866.       else
  1867.       begin { A File }
  1868.         { Set up the default color scheme for files }
  1869.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1870.          ButtonHLColor , ButtonSColor , TextColor );
  1871.         { If found create a new FileIconPanel on the imported scrollbox }
  1872.         { Note sending 0 ExtraData parameter to indicate file not drive }
  1873.         TheFIP := TFileIconPanel.Create( Self );
  1874.         TheFIP.Parent := Self;
  1875.         TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1876.          (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1877.           7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1878.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1879.         { Increment column counter and move to new row if past limit }
  1880.         XCounter := XCounter + 1;
  1881.         if XCounter > MaxIconsInARow then
  1882.         begin
  1883.           XCounter := 1;
  1884.           YCounter := YCounter + 1;
  1885.         end;
  1886.       end;
  1887.     end;
  1888.   end;
  1889.   { Call FindClose for Windows NT/Windows 95 compatibility }
  1890.   FindClose( TheSR );
  1891.   { Set up the variables to do recursive calls on all directories}
  1892.   Finished := false;
  1893.   ModifiedDirectory := ExtractFilePath( TheWorkingdirectory ) + '*.*';
  1894.   { Make the call to FindFirst set to get any file, ignore result }
  1895.   TheResult := FindFirst( ModifiedDirectory , faDirectory , TheSR );
  1896.   while not Finished do
  1897.   begin
  1898.     { Make call to FindNext, using only SearchRecord from FindFirst }
  1899.     TheResult := FindNext( TheSR );
  1900.     { A -1 result means no more files so exit }
  1901.     if TheResult < 0 then finished := true
  1902.     else
  1903.     begin
  1904.       if TheSR.Name <> '..' then { Ignore backup in this case }
  1905.       begin
  1906.         { Do second check due to bug in FindNext }
  1907.         if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory )
  1908.         = faDirectory ) then
  1909.         begin
  1910.           { Set up modified directory to recurse into }
  1911.           ModifiedDirectory := ExtractFilePath( TheStoredWorkingDirectory ) +
  1912.            TheSR.Name + '\' + FileMask;
  1913.           { Perform the recursion }
  1914.           RecursiveFileSearch( ModifiedDirectory , XCounter , YCounter );
  1915.         end;
  1916.       end;
  1917.     end;
  1918.   end;
  1919. end;
  1920.  
  1921. begin
  1922.   { Keep the scrollbox from updating during refresh }
  1923.   Visible := false;
  1924.   { Make the clear call }
  1925.   ClearTheFIPs;
  1926.   XCounter := 1;
  1927.   YCounter := 1;
  1928.   { Get the drives for the current machine }
  1929.   AddDriveIcons( XCounter , YCounter );
  1930.   RecursiveFileSearch( TheStartingDirectory , XCounter , YCounter );
  1931.   { Make the scrollbox visible again }
  1932.   Visible := true;
  1933. end;
  1934.  
  1935. end.
  1936.